#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ScoreProvider : CY.GFive.Core.Providers.DALProvider.IScoreProvider
	{
		#region Sql
		private static readonly string SqlSelectScore = "SELECT [Id], [CourseCode],[CourseCate],[YearNum],[Grade],[Term],[ClassCode],[UsualScore],[MiddleScore],[EndScore],[TotalScore],[ScoreType],[StdCode] FROM [Score] WHERE  [Id]=@Id";

		private static readonly string SqlInsertScore = "INSERT INTO [Score]([CourseCode],[CourseCate],[YearNum],[Grade],[Term],[ClassCode],[UsualScore],[MiddleScore],[EndScore],[TotalScore],[ScoreType],[StdCode]) values(@CourseCode,@CourseCate,@YearNum,@Grade,@Term,@ClassCode,@UsualScore,@MiddleScore,@EndScore,@TotalScore,@ScoreType,@StdCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateScore = "UPDATE [Score] SET [CourseCode] = @CourseCode,[CourseCate] = @CourseCate,[YearNum] = @YearNum,[Grade] = @Grade,[Term] = @Term,[ClassCode] = @ClassCode,[UsualScore] = @UsualScore,[MiddleScore] = @MiddleScore,[EndScore] = @EndScore,[TotalScore] = @TotalScore,[ScoreType] = @ScoreType,[StdCode] = @StdCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteScore = "DELETE [Score] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllScore = "SELECT [Id], [CourseCode],[CourseCate],[YearNum],[Grade],[Term],[ClassCode],[UsualScore],[MiddleScore],[EndScore],[TotalScore],[ScoreType],[StdCode] FROM [Score]";
		#endregion

		#region IScoreProvider Members
		public Core.Business.Score Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectScore);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Score score = new Core.Business.Score();

				if (!reader.IsDBNull(0)) score.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) score.CourseCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) score.CourseCate = reader.GetString(2);
				if (!reader.IsDBNull(3)) score.YearNum = reader.GetString(3);
				if (!reader.IsDBNull(4)) score.Grade = reader.GetString(4);
				if (!reader.IsDBNull(5)) score.Term = reader.GetInt32(5);
				if (!reader.IsDBNull(6)) score.ClassCode = reader.GetString(6);
				if (!reader.IsDBNull(7)) score.UsualScore = reader.GetDecimal(7);
				if (!reader.IsDBNull(8)) score.MiddleScore = reader.GetDecimal(8);
				if (!reader.IsDBNull(9)) score.EndScore = reader.GetDecimal(9);
				if (!reader.IsDBNull(10)) score.TotalScore = reader.GetDecimal(10);
				if (!reader.IsDBNull(11)) score.ScoreType = reader.GetString(11);
				if (!reader.IsDBNull(12)) score.StdCode = reader.GetString(12);

				return score;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Score score)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@CourseCode", SqlDbType.NVarChar, score.CourseCode);
			sql.AddParameter("@CourseCate", SqlDbType.NVarChar, score.CourseCate);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, score.YearNum);
			sql.AddParameter("@Grade", SqlDbType.NVarChar, score.Grade);
			sql.AddParameter("@Term", SqlDbType.Int, score.Term);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, score.ClassCode);
			sql.AddParameter("@UsualScore", SqlDbType.Decimal, score.UsualScore);
			sql.AddParameter("@MiddleScore", SqlDbType.Decimal, score.MiddleScore);
			sql.AddParameter("@EndScore", SqlDbType.Decimal, score.EndScore);
			sql.AddParameter("@TotalScore", SqlDbType.Decimal, score.TotalScore);
			sql.AddParameter("@ScoreType", SqlDbType.NVarChar, score.ScoreType);
			sql.AddParameter("@StdCode", SqlDbType.NVarChar, score.StdCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertScore);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) score.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Score score)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@CourseCode", SqlDbType.NVarChar, score.CourseCode);
			sql.AddParameter("@CourseCate", SqlDbType.NVarChar, score.CourseCate);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, score.YearNum);
			sql.AddParameter("@Grade", SqlDbType.NVarChar, score.Grade);
			sql.AddParameter("@Term", SqlDbType.Int, score.Term);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, score.ClassCode);
			sql.AddParameter("@UsualScore", SqlDbType.Decimal, score.UsualScore);
			sql.AddParameter("@MiddleScore", SqlDbType.Decimal, score.MiddleScore);
			sql.AddParameter("@EndScore", SqlDbType.Decimal, score.EndScore);
			sql.AddParameter("@TotalScore", SqlDbType.Decimal, score.TotalScore);
			sql.AddParameter("@ScoreType", SqlDbType.NVarChar, score.ScoreType);
			sql.AddParameter("@StdCode", SqlDbType.NVarChar, score.StdCode);
			sql.AddParameter("@Id", SqlDbType.Int, score.Id);
			sql.ExecuteSql(SqlUpdateScore);
		}

		public void Delete(Core.Business.Score score)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, score.Id);
			sql.ExecuteSql(SqlDeleteScore);
		}

		public IList<Core.Business.Score> GetAllScore()
		{
			IList<Core.Business.Score> scorelist = new List<Core.Business.Score>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllScore);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Score score = new Core.Business.Score();

					if (!reader.IsDBNull(0)) score.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) score.CourseCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) score.CourseCate = reader.GetString(2);
					if (!reader.IsDBNull(3)) score.YearNum = reader.GetString(3);
					if (!reader.IsDBNull(4)) score.Grade = reader.GetString(4);
					if (!reader.IsDBNull(5)) score.Term = reader.GetInt32(5);
					if (!reader.IsDBNull(6)) score.ClassCode = reader.GetString(6);
					if (!reader.IsDBNull(7)) score.UsualScore = reader.GetDecimal(7);
					if (!reader.IsDBNull(8)) score.MiddleScore = reader.GetDecimal(8);
					if (!reader.IsDBNull(9)) score.EndScore = reader.GetDecimal(9);
					if (!reader.IsDBNull(10)) score.TotalScore = reader.GetDecimal(10);
					if (!reader.IsDBNull(11)) score.ScoreType = reader.GetString(11);
					if (!reader.IsDBNull(12)) score.StdCode = reader.GetString(12);

					score.MarkOld();
					scorelist.Add(score);
				}
				reader.Close();
			}
			return scorelist;
		}
		#endregion
	}
}
